﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Core;
using Web.Prop;

namespace Web.pages
{
    public partial class profileinfomation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    var o = Dao.USERDao.Init().Select(
                                                    new USER()
                                                    {
                                                        UID = ((USER)Session["UserInfo"]).UID
                                                    }
                                                    ).First();
                    if (o != null)
                    {
                        txFName.Text = o.FNAME;
                        txLNAME.Text = o.LNAME;
                        txEmail.Text = o.EMAIL;
                        txEmail.Enabled = false;
                        txTel.Text = o.TEL;
                        txCompany.Text = o.COMPANY;
                        txDeparment.Text = o.DEPT;
                        txPosition.Text = o.POSITION;
                    }
                }
            }
            catch (Exception ex)
            {
                Dao.TransactionlogDao.Init().Add(new Transactionlog()
                {
                    UID = ((USER)Session["UserInfo"]).UID
                    ,
                    EVENT = Core.Events.Error.ToString()
                    ,
                    LASTIP = Request.ServerVariables["REMOTE_ADDR"]
                    ,
                    UPDATE = DateTime.Now
                    ,
                    DESCRIPT = ex.Message
                });
            }
            upl.Update();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
                if (String.IsNullOrEmpty(txPasswordOld.Text.Trim()))
                {
                    Alert.Message(this.Page, "กรุณากรอกรหัสผ่าน");
                }
                else if (String.IsNullOrEmpty(txPasswordNew.Text.Trim()))
                {
                    Alert.Message(this.Page, "กรุณากรอกรหัสผ่าน");
                }
                else if (String.IsNullOrEmpty(txConfirm.Text.Trim()))
                {
                    Alert.Message(this.Page, "กรุณายืนยันรหัสผ่าน");
                }

                else if (!txPasswordNew.Text.Trim().Equals(txConfirm.Text.Trim()))
                {
                    Alert.Message(this.Page, "รหัสผ่านไม่ตรงกัน");
                }
                else if (String.IsNullOrEmpty(txFName.Text.Trim()))
                {
                    Alert.Message(this.Page, "กรุณากรอกชื่อ");
                }
                else if (String.IsNullOrEmpty(txLNAME.Text.Trim()))
                {
                    Alert.Message(this.Page, "กรุณากรอกนามสกุล");
                }
                else if (String.IsNullOrEmpty(txEmail.Text.Trim()))
                {
                    Alert.Message(this.Page, "กรุณากรอก Email");
                }
                else if (String.IsNullOrEmpty(txTel.Text.Trim()))
                {
                    Alert.Message(this.Page, "กรุณากรอกเบอร์โทร");
                }
                else if (String.IsNullOrEmpty(txCompany.Text.Trim()))
                {
                    Alert.Message(this.Page, "กรุณากรอกชื่อบริษัท");
                }
                else if (String.IsNullOrEmpty(txDeparment.Text.Trim()))
                {
                    Alert.Message(this.Page, "กรุณากรอกชื่อแผนกงาน");
                }
                else if (String.IsNullOrEmpty(txPosition.Text.Trim()))
                {
                    Alert.Message(this.Page, "กรุณากรอกชื่อตำแหน่งงาน");
                }
                else
                {
                    var o = Dao.USERDao.Init().Select(
                                                   new USER()
                                                   {
                                                       UID = ((USER)Session["UserInfo"]).UID
                                                            ,
                                                       UWD = ((USER)Session["UserInfo"]).UWD
                                                        ,
                                                       PWD = Core.Cipher.EncrytData(txPasswordOld.Text.Trim())
                                                   }
                                                   );
                    if (o.Count > 0)
                    {
                        Boolean result = Dao.USERDao.Init().Update(new USER()
                        {
                            UID = ((USER)Session["UserInfo"]).UID
                                ,
                            UWD = ((USER)Session["UserInfo"]).UWD
                                ,
                            PWD = Core.Cipher.EncrytData(txPasswordNew.Text.Trim())
                                ,
                            FNAME = txFName.Text.Trim()
                                ,
                            LNAME = txLNAME.Text.Trim()
                                ,
                            EMAIL = txEmail.Text.Trim()
                                ,
                            TEL = txTel.Text.Trim()
                                ,
                            COMPANY = txCompany.Text.Trim()
                                ,
                            DEPT = txDeparment.Text.Trim()
                                ,
                            POSITION = txPosition.Text.Trim()
                            ,
                            UPDATE = DateTime.Now
                        });

                        //
                        Dao.UserlogDao.Init().Add(new Userlog()
                        {
                            UID = ((USER)Session["UserInfo"]).UID
                            ,
                            LASTIP = Request.ServerVariables["REMOTE_ADDR"]
                            ,
                            UPDATE = DateTime.Now
                            ,
                            EVENT = Core.Events.ChangePassword.ToString()
                        });

                        //
                        Response.Redirect("default.aspx");
                    }
                    else
                    {
                        Alert.Message(this.Page, "รหัสผ่านเดิมไม่ถูกต้อง");
                    }

                }
            }
            catch (Exception ex)
            {
                Dao.TransactionlogDao.Init().Add(new Transactionlog()
                {
                    UID = ((USER)Session["UserInfo"]).UID
                    ,
                    EVENT = Core.Events.Error.ToString()
                    ,
                    LASTIP = Request.ServerVariables["REMOTE_ADDR"]
                    ,
                    UPDATE = DateTime.Now
                    ,
                    DESCRIPT = ex.Message
                });
            }
            upl.Update();
        }

    }
}